" ------------------------
" shortmess
" ------------------------
if has('patch-9.0.1568')
    set sms
endif
if has('patch-8.1.1270')
    set shortmess+=S
endif
if has('patch-7.4.1829')
    set shortmess+=a
    set shortmess+=c
endif
" --------------------------
" complete engine
" --------------------------
set completeopt=menu,menuone
try
    set completeopt+=noselect
    set completeopt+=noinsert
    if exists('+completepopup') != 0
        set completeopt+=popup
        set completepopup=align:menu,border:off,highlight:WildMenu
    endif
catch
    call AddRequire('nocomplete')
endtry
if Require('nocomplete')
    let g:complete_engine = ''
elseif Require('mcm')
    let g:complete_engine = 'mcm'
elseif Require('apm')
    if v:version >= 800
        let g:complete_engine = 'apm'
    else
        let s:smart_engine_select = 1
    endif
elseif Require('cmp')
    if has('nvim-0.9')
        let g:complete_engine = 'cmp'
    else
        let s:smart_engine_select = 1
    endif
elseif Require('coc')
    if g:node_version > 14.14 && (has('nvim-0.8.1') || has('patch-8.2.0750'))
        let g:complete_engine = 'coc'
    else
        let s:smart_engine_select = 1
    endif
else
    let s:smart_engine_select = 1
endif
if get(s:, 'smart_engine_select', 0)
    if has('nvim-0.9')
        let g:complete_engine = 'cmp'
    elseif g:node_version > 14.14 && has('nvim-0.8.1')
        let g:complete_engine = 'coc'
    else
        let g:complete_engine = 'mcm'
    endif
endif
" ------------------------------
" complete_engine
" ------------------------------
if g:complete_engine == 'cmp'
    " complete related
    PlugAdd 'hrsh7th/nvim-cmp'
    PlugAdd 'hrsh7th/cmp-nvim-lsp'
    PlugAdd 'hrsh7th/cmp-nvim-lua'
    PlugAdd 'hrsh7th/cmp-path'
    PlugAdd 'hrsh7th/cmp-buffer'
    PlugAdd 'hrsh7th/cmp-cmdline'
    PlugAdd 'hrsh7th/cmp-nvim-lsp-signature-help'
    PlugAdd 'L3MON4D3/luasnip'
    PlugAdd 'saadparwaiz1/cmp_luasnip'
    PlugAdd 'onsails/lspkind-nvim'
    " lsp related
    PlugAdd 'neovim/nvim-lspconfig'
    PlugAdd 'williamboman/mason.nvim'
    PlugAdd 'williamboman/mason-lspconfig.nvim'
    PlugAdd 'mfussenegger/nvim-lint'
    PlugAdd 'DNLHC/glance.nvim'
    PlugAdd 'gfanto/fzf-lsp.nvim'
    PlugAdd 'kosayoda/nvim-lightbulb'
    PlugAdd 'josa42/nvim-lightline-lsp'
    PlugAdd 'lvimuser/lsp-inlayhints.nvim'
    PlugAdd 'camilledejoye/nvim-lsp-selection-range'
    PlugAdd 'fgheng/winbar.nvim'
    " ui && function
    PlugAdd 'MunifTanjim/nui.nvim'
    PlugAdd 'nvim-lua/plenary.nvim'
elseif g:complete_engine == 'coc'
    if get(g:, 'coc_install_release', 0) == 0
        PlugAdd 'neoclide/coc.nvim', {'branch': 'master', 'do': 'npm ci'}
    else
        PlugAdd 'neoclide/coc.nvim', {'branch': 'release'}
    endif
    PlugAddOpt 'coc-fzf'
endif
if g:complete_engine != '' && g:complete_engine != 'apm'
    PlugAdd 'hrsh7th/vim-vsnip'
    PlugAdd 'rafamadriz/friendly-snippets'
    if g:complete_engine == 'mcm'
        PlugAdd 'hrsh7th/vim-vsnip-integ'
    endif
endif
" ------------------------------
" check tool
" ------------------------------
if g:complete_engine == 'cmp'
    let g:check_tool = 'lsp'
elseif g:complete_engine == 'coc'
    if g:python_version > 3.06 && Require('ale')
        let g:check_tool = 'ale'
    else
        let g:check_tool = 'coc'
    endif
elseif g:python_version > 3.06 && v:version >= 800
    let g:check_tool = 'ale'
else
    let g:check_tool = ''
endif
if g:check_tool == 'ale'
    PlugAdd 'dense-analysis/ale'
    PlugAdd 'maximbaz/lightline-ale'
endif
" ------------------------------
" debug tool
" ------------------------------
if g:python_version > 3.08 && (v:version >= 802 && Require('debug') || has('nvim-0.8.1') && Require('vimspector'))
    let vimspector_install = " ./install_gadget.py --update-gadget-config"
    PlugAdd 'puremourning/vimspector', {'do': g:python_path . vimspector_install}
elseif has('nvim-0.8.1') && Require('debug')
    PlugAdd 'mfussenegger/nvim-dap'
    PlugAdd 'rcarriga/nvim-dap-ui'
endif
if has('nvim-0.8') && Require('jupynium') && g:python_version > 3.07
    PlugAdd 'kiyoon/jupynium.nvim', {'do': get(g:, 'jupynium_install', 'pip3 install --user .')}
endif
" ------------------------------
" format tools
" ------------------------------
PlugAdd 'sbdchd/neoformat'
" ----------------------------
" extend Installed function
" ----------------------------
function! InstalledFzf() abort
    return Installed('fzf', 'fzf.vim')
endfunction
function! InstalledCoc() abort
    return Installed('coc.nvim', 'coc-fzf', 'friendly-snippets') && InstalledFzf()
endfunction
function! InstalledNvimLsp() abort
    return Installed(
                \ 'nvim-lspconfig',
                \ 'mason.nvim',
                \ 'mason-lspconfig.nvim',
                \ 'nvim-lsp-selection-range',
                \ 'nvim-lightbulb',
                \ 'fzf-lsp.nvim',
                \ 'glance.nvim',
                \ 'winbar.nvim',
                \ )
endfunction
function! InstalledCmp() abort
    return Installed(
                \ 'nvim-cmp',
                \ 'cmp-path',
                \ 'cmp-buffer',
                \ 'cmp-cmdline',
                \ 'cmp-nvim-lsp',
                \ 'cmp-nvim-lua',
                \ 'cmp-nvim-lsp-signature-help',
                \ 'cmp_luasnip',
                \ 'luasnip',
                \ 'friendly-snippets',
                \ 'nvim-lint',
                \ 'lspkind-nvim',
                \ )
endfunction
function! InstalledAdvCompEng() abort
    return InstalledCoc() || InstalledCmp() && InstalledNvimLsp()
endfunction
